package com.three.storemanage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.three.core.entity.FrontUser;
import com.three.core.entity.StoreEvaluate;
import com.three.core.entity.User;
import com.three.core.vo.R;
import com.three.storemanage.feign.UserFeign;
import com.three.storemanage.mapper.FrontUserMapper;
import com.three.storemanage.mapper.StoreEvaluateMapper;
import com.three.storemanage.service.EvaluateService;
import com.three.storemanage.vo.EvaluateVo;
import com.three.storemanage.vo.SearchForm;
import com.three.storemanage.vo.StoreEvaluateVo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @FileName: EvaluateServiceImpl
 * @Description:
 * @Author: GYX
 * @CreateTime: 2025/3/13 13:57
 * @Version: 1.0.0
 */
@Service
public class EvaluateServiceImpl implements EvaluateService {

    @Autowired
    private StoreEvaluateMapper storeEvaluateMapper;

    @Autowired
    private UserFeign userFeign;
    @Autowired
    private HttpServletRequest request;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private FrontUserMapper frontUserMapper;
    @Override
    public R<Page<EvaluateVo>> findEvaluateByCondition(SearchForm searchForm) {
        String token = request.getHeader("token");
        Object userName = redisTemplate.opsForHash().get("user::login::" + token, "name");
        R<User> feign = userFeign.getByName((String) userName);
        User user = feign.getData();
        Integer userId = user.getUserId();
        Page<EvaluateVo> page = new Page<>(searchForm.getCurrentPage(), searchForm.getPageSize());
        Page<EvaluateVo> records = storeEvaluateMapper.findEvaluateByCondition(page,searchForm.getStoreName(),searchForm.getUserName(),userId);
        return new R<>(200,"查询成功",records);
    }

    @Override
    public R<List<FrontUser>> queryAll() {
        List<FrontUser> frontUsers = frontUserMapper.queryAll();
        return new R<>(200,"查询成功",frontUsers);
    }

    @Override
    public R deleteEvaluate(Integer evaluateId) {
        int i = storeEvaluateMapper.deleteById(evaluateId);
        if (i>0){
            return new R<>(200,"删除成功",null);
        }else {
            return new R<>(500,"删除失败",null);
        }
    }

    @Override
    public R addEvaluate(StoreEvaluate storeEvaluate) {
        String token = request.getHeader("token");
        Object phone = redisTemplate.opsForHash().get("user::login::" + token, "name");
        FrontUser frontUser = frontUserMapper.selectByPhone((String) phone);
        Integer userId = frontUser.getUserId();
        //storeEvaluate.setUserId(String.valueOf(userId));
        //设置当前的系统时间
        storeEvaluate.setEvaluateTime(new Date());
        int i = storeEvaluateMapper.insertSelective(storeEvaluate);
        if (i>0){
            return new R<>(200,"添加成功",null);
        }else {
            return new R<>(500,"添加失败",null);
        }
    }

    @Override
    public R updateEvaluate(StoreEvaluate storeEvaluate) {
        int i = storeEvaluateMapper.updateByPrimaryKeySelective(storeEvaluate);
        if (i>0){
            return new R<>(200,"修改成功",null);
        }else {
            return new R<>(500,"修改失败",null);
        }
    }

    @Override
    public R<StoreEvaluateVo> selectEvaluateByStoreName(String storeName) {
        List<StoreEvaluate> storeEvaluate = storeEvaluateMapper.selectEvaluateByStoreName(storeName);
        StoreEvaluateVo storeEvaluateVo = new StoreEvaluateVo();
        int rating = 0;
        storeEvaluateVo.setCollectPeople(storeEvaluate.size());
        for (StoreEvaluate s: storeEvaluate) {
            rating += s.getStar();
        }
        storeEvaluateVo.setRating(rating/storeEvaluate.size());
        System.out.println(storeEvaluateVo+"==============");
        return new R<>(200,"查询成功",storeEvaluateVo);
    }

    @Override
    public R<List<StoreEvaluate>> selectEvaluateByStoreId(Integer storeId) {
        List<StoreEvaluate> storeEvaluate = storeEvaluateMapper.selectEvaluateByStoreId(storeId);
        return new R<>(200,"查询成功",storeEvaluate);
    }

}
